home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / gjr / cmplrtst.lha / sort / fxmsort.scm < prev    next >
Encoding:
Text File  |  1990-03-27  |  3.5 KB  |  118 lines

  1. ;;; -*-Scheme-*-
  2. ;;;
  3. ;;;    $Header: msort.scm,v 13.42 87/11/21 18:06:51 GMT jinx Exp $
  4. ;;;
  5. ;;;    Copyright (c) 1987 Massachusetts Institute of Technology
  6. ;;;
  7. ;;;    This material was developed by the Scheme project at the
  8. ;;;    Massachusetts Institute of Technology, Department of
  9. ;;;    Electrical Engineering and Computer Science.  Permission to
  10. ;;;    copy this software, to redistribute it, and to use it for any
  11. ;;;    purpose is granted, subject to the following restrictions and
  12. ;;;    understandings.
  13. ;;;
  14. ;;;    1. Any copy made of this software must include this copyright
  15. ;;;    notice in full.
  16. ;;;
  17. ;;;    2. Users of this software agree to make their best efforts (a)
  18. ;;;    to return to the MIT Scheme project any improvements or
  19. ;;;    extensions that they make, so that these may be included in
  20. ;;;    future releases; and (b) to inform MIT of noteworthy uses of
  21. ;;;    this software.
  22. ;;;
  23. ;;;    3.  All materials developed as a consequence of the use of
  24. ;;;    this software shall duly acknowledge such use, in accordance
  25. ;;;    with the usual standards of acknowledging credit in academic
  26. ;;;    research.
  27. ;;;
  28. ;;;    4. MIT has made no warrantee or representation that the
  29. ;;;    operation of this software will be error-free, and MIT is
  30. ;;;    under no obligation to provide any services, by way of
  31. ;;;    maintenance, update, or otherwise.
  32. ;;;
  33. ;;;    5.  In conjunction with products arising from the use of this
  34. ;;;    material, there shall be no use of the name of the
  35. ;;;    Massachusetts Institute of Technology nor of any adaptation
  36. ;;;    thereof in any advertising, promotional, or sales literature
  37. ;;;    without prior written consent from MIT in each case.
  38. ;;;
  39.  
  40. ;;;; Merge Sort
  41.  
  42. (declare (usual-integrations 1+ -1+ + = < > integer-divide)
  43.      (integrate-primitive-procedures
  44.       (-1+ minus-one-plus-fixnum)
  45.       (1+ one-plus-fixnum)
  46.       (+ plus-fixnum)
  47.       (= equal-fixnum?)
  48.       (< less-than-fixnum?)
  49.       (> greater-than-fixnum?)
  50.       (integer-divide divide-fixnum)))
  51.  
  52. ;; Functional and unstable
  53.  
  54. (define (sort obj pred)
  55.   (define (loop l)
  56.     (if (and (pair? l) (pair? (cdr l)))
  57.     (split l '() '())
  58.     l))
  59.  
  60.   (define (split l one two)
  61.     (if (pair? l)
  62.     (split (cdr l) two (cons (car l) one))
  63.     (merge (loop one) (loop two))))
  64.  
  65.   (define (merge one two)
  66.     (cond ((null? one) two)
  67.       ((pred (car two) (car one))
  68.        (cons (car two)
  69.          (merge (cdr two) one)))
  70.       (else
  71.        (cons (car one)
  72.          (merge (cdr one) two)))))
  73.  
  74.   (cond ((or (pair? obj) (null? obj))
  75.      (loop obj))
  76.     ((vector? obj)
  77.      (sort! (vector-copy obj) pred))
  78.     (else
  79.      (error "sort: argument should be a list or vector" obj))))
  80.  
  81. ;; This merge sort is stable for partial orders (for predicates like
  82. ;; <=, rather than like <).
  83.  
  84. (define (sort! v pred)
  85.   (declare (integrate-operator quo))
  86.  
  87.   (define (quo x y)
  88.     (declare (integrate x y))
  89.     (car (integer-divide x y)))
  90.  
  91.   (define (sort-internal! vec temp low high)
  92.     (if (< low high)
  93.     (let* ((middle (quo (+ low high) 2))
  94.            (next (1+ middle)))
  95.       (sort-internal! temp vec low middle)
  96.       (sort-internal! temp vec next high)
  97.       (let loop ((p low) (p1 low) (p2 next))
  98.         (if (not (> p high))
  99.         (cond ((> p1 middle)
  100.                (vector-set! vec p (vector-ref temp p2))
  101.                (loop (1+ p) p1 (1+ p2)))
  102.               ((or (> p2 high)
  103.                (pred (vector-ref temp p1)
  104.                  (vector-ref temp p2)))
  105.                (vector-set! vec p (vector-ref temp p1))
  106.                (loop (1+ p) (1+ p1) p2))
  107.               (else
  108.                (vector-set! vec p (vector-ref temp p2))
  109.                (loop (1+ p) p1 (1+ p2)))))))))
  110.  
  111.   (if (not (vector? v))
  112.       (error "sort!: argument not a vector" v))
  113.  
  114.   (sort-internal! v
  115.           (vector-copy v)
  116.           0
  117.           (-1+ (vector-length v)))
  118.   v)